package com.chiry.business.controller;

import com.chiry.business.entity.Result;
import com.chiry.business.remote.ObjStorageClient;
import com.chiry.business.until.FileUtil;
import com.chiry.business.until.command.CMDUtil;
import com.ruoyi.common.core.text.UUID;
import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

/**
 * @author fyx
 * @version 1.0
 * @date 2021/8/17 15:11
 */
@RestController
@RequestMapping("/model/visual")
public class VisualPredicateController {
    final static Logger logger = LoggerFactory.getLogger(VisualPredicateController.class);
    public final static String BUCKET_NAME = "aws-cn-insect-images";
    @Autowired
    private ObjStorageClient objStorageClient;

    @Value("${custom.config.command.visual}")
    public String visualCmd;

    @PostMapping("/by-url")
    public Result<String> predicateImageByUrl(@RequestParam("imgUrl") String imgUrl ,@RequestParam("modelUrl") String modelUrl) throws ExecutionException, InterruptedException {
        String predicateReult=null;
 //       modelUrl = "C:\\Users\\fyxdd\\anaconda3\\envs\\my_paddlex\\model";  +visualize

        Result<String> upload = null;
        MultipartFile multipartFile = null;
        int dirPrefix = Math.abs(UUID.getRandom().nextInt());
        System.out.println(dirPrefix);
        String cmd = visualCmd +
                " " +imgUrl+
                " "+dirPrefix;
        logger.info("commad:"+cmd);
        String result = null;
        try {
            //.\visualize\1491747171\visualize_1629184201752.jpg
            result = CMDUtil.excuteCMDCommand(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
        modelUrl = "C:\\Users\\fyxdd\\anaconda3\\envs\\my_paddlex\\model\\insectMonitoring\\v1\\visualize";  //目前未做产业化配置顾给配置数据
        String resultPath = FileUtil.printRecentFile(new File(modelUrl));
        logger.info("resultPath:"+resultPath);
        FileInputStream inputStream = null;
        try {
            File file = new File(resultPath);
            inputStream = new FileInputStream(file);
            multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                    ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        upload= objStorageClient.upload(multipartFile, 1, BUCKET_NAME);
        System.out.println(result);
        return upload;
    }

}
